package com.runtime.hongyunuser.service.Impl;


import com.runtime.hongyunuser.bean.Productimages;
import com.runtime.hongyunuser.bean.Products;
import com.runtime.hongyunuser.bean.Userfavorites;
import com.runtime.hongyunuser.mapper.ProductMapper;
import com.runtime.hongyunuser.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Iterator;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Products> selectProduct(String keyword) {
        Products products = productMapper.selectProductByProductNumber(keyword);
        List<Products> products2 = productMapper.selectProductByProductName(keyword);
        //移除模糊中的相同数据
        if(null != products){
            Iterator<Products> iterator = products2.iterator();
            while (iterator.hasNext()){
                Products p = iterator.next();
                if(p.getProductId() == products.getProductId()){
                    iterator.remove();
                }
            }
            products2.add(0,products);
        }
        return products2;
    }
//获取收藏夹
    @Override
    public List<Userfavorites> selectProductFavorite(long uesrId) {
        return productMapper.selectProductFavorite(uesrId);
    }
//移除收藏夹
    @Override
    public int deleteProductFavorite(long uesrId, long productId) {
        return productMapper.deleteFavorite(uesrId,productId);
    }
//加入收藏夹
    @Override
    public int addProductFavorite(long uesrId, long productId) {
        return productMapper.addProductFavorite(uesrId,productId);
    }

//    @Override
//    public int deleteProductFavorite(long uesrId, long productId) {
//        // 执行删除
//     return    productMapper.deleteFavorite(uesrId,productId);
//
//
//
//    }


    public List<Products> getNewProducts() {
        return productMapper.getNewProducts();
    }

    @Override
    public List<Products> getFeaturedProducts() {
        return productMapper.getFeaturedProducts();
    }

    @Override
    public List<Products> getValueProducts() {
        return productMapper.getValueProducts();
    }

    @Override
    public List<Products> getDeviceProducts() {
        return productMapper.getDeviceProducts();
    }

    @Override
    public List<Products> getPromotionProducts() {
        return productMapper.getPromotionProducts();
    }

    @Override
    public Productimages getProductImages(Integer productId) {
        return productMapper.getProductImages(productId);
    }


    public ProductServiceImpl(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    @Override
    public Products getProductById(long productId) {
        return productMapper.getProductById(productId);
    }

}
